<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * Class VoteRater
 *
 * @package App\Models
 * @property int $id
 * @property int $vote_id
 * @property int $rater_id
 * @property int $confirmed
 * @property int $poll_max
 * @property int $poll_min
 * @property-read \App\Models\Vote $vote
 * @property-read \App\Models\Rater $rater
 * @method static \Illuminate\Database\Query\Builder|\App\Models\VoteRater whereVoteId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\VoteRater whereRaterId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\VoteRater whereConfirmed($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\VoteRater wherePollMax($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\VoteRater wherePollMin($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\VoteRater whereId($value)
 * @mixin \Eloquent
 */
class VoteRater extends Model
{
    protected $table = 'vote_rater';

    public $timestamps = false;

    public $voteCount;

    /**
     * 投票池
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function vote()
    {
        return $this->belongsTo(Vote::class, 'vote_id');
    }

    /**
     * 评委
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function rater()
    {
        return $this->belongsTo(Rater::class, 'rater_id');
    }

    /**
     * 投票统计
     * @return mixed
     */
    public function getVoteCount()
    {
        if ($this->voteCount != null) {
            return $this->voteCount;
        } else {
            $this->voteCount = VoteDetail::where('vote_id', $this->vote_id)
                ->where('rater_id', $this->rater_id)
                ->count();
            return $this->voteCount;
        }
    }


    /**
     * 评委查看进度
     * @return mixed
     */
    public function getVoteWorksClickedCount()
    {
        return VoteWorksClick::where(['vote_id' => $this->vote_id , 'rater_id' => $this->rater_id])->count();
    }

    /**
     * 剩余可投票数
     * @return int
     */
    public function getRestPoll()
    {
        return $this->poll_max - $this->getVoteCount();
    }
}